﻿using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using eCommerce.core.AdminUser;
using eCommerce.core.Helpers;

namespace eCommerce.core.AdminMenu
{
    public class AdminMenuController
    {        
        public static void AdminMenu_GetTree(List<AdminMenuObject> arrayList, int level, int parentId)
        {
            var menus = AdminMenu_GetByParentId(parentId);

            foreach (var m in menus)
            {
                var prefix = string.Empty;

                for (var i = 0; i < level; i++)
                {
                    prefix += "--- ";
                }

                m.Name = prefix == string.Empty ? string.Format("{0}", m.Name.Trim()) : prefix + m.Name;

                arrayList.Add(m);

                level++;
                AdminMenu_GetTree(arrayList, level, m.AdminMenuId);
                level--;
            }
        }

        public static int AdminMenu_GetStatistics(int status)
        {
            return AdminMenuDAL.AdminMenu_GetStatistics(status);
        }

        public static AdminMenuObject AdminMenu_GetById(int id)
        {
            return ObjectHelper.FillObject<AdminMenuObject>(AdminMenuDAL.AdminMenu_GetById(id));
        }

        public static int AdminMenu_Create(AdminMenuObject o)
        {
            return AdminMenuDAL.AdminMenu_Create(o);
        }

        public static void AdminMenu_Update(AdminMenuObject o)
        {
            AdminMenuDAL.AdminMenu_Update(o);
        }

        public static void AdminMenu_Delete(int adminMenuId)
        {
            AdminMenuDAL.AdminMenu_Delete(adminMenuId);
        }

        public static List<AdminMenuObject> AdminMenu_GetByParentId(int parentId)
        {
            return ObjectHelper.FillCollection<AdminMenuObject>(AdminMenuDAL.AdminMenu_GetByParentId(parentId));
        }

        public static List<AdminMenuObject> AdminMenu_GetAllByStatusAndUserId(int status, int userId)
        {            
            var cacheName = string.Format("menu_{0}_{1}", HttpContext.Current.User.Identity.Name, status);
            if (HttpContext.Current.Cache[cacheName] != null)
                return (List<AdminMenuObject>)HttpContext.Current.Cache[cacheName];
            
            var lst = ObjectHelper.FillCollection<AdminMenuObject>(AdminMenuDAL.AdminMenu_GetAllByStatusAndUserId(status, userId));
            HttpContext.Current.Cache.Insert(cacheName, lst);
            return lst;
        }

        #region Bind AdminMenu HTML
        public static string GetMenuHtml(AdminUserObject currentUser)
        {
            //var lst = AdminMenu_GetAllByStatusAndUserId(AdminMenuObject.IsVisibled + AdminMenuObject.IsVisibledWhenRun, currentUser.AdminUserId);
            var lst = AdminMenu_GetAllByStatusAndUserId(AdminMenuObject.IsVisibled, currentUser.AdminUserId);
                        
            var ret = new StringBuilder();
            ret.Append("<ul class=\"mainNav\">");
            foreach (var m in lst)
            {
                if (m.ParentId != 0) continue;
                var childList = GetChildItems(lst, m);
                if (childList.Count > 0)
                {                    
                    ret.AppendFormat("<li class=\"dropdown\"><a href=\"#\"><span><img src=\"/img/icon/mainNav/{0}\"/> {1} ({2})</span></a><ul>",
                                              m.CtrlKey, m.Name, childList.Count);
                    
                    ret.Append(RecursionItems(lst, childList));
                    ret.Append("</ul></li>");
                }
                else
                    ret.AppendFormat("<li><a href=\"/Default.aspx?cId={0}\"><span><img src=\"/img/icon/mainNav/{1}\"/>{2}</span></a></li>",
                                            m.AdminMenuId, m.CtrlKey, m.Name);
            }
            ret.Append("</ul>");
            return ret.ToString();
        }

        private static List<AdminMenuObject> GetChildItems(IEnumerable<AdminMenuObject> lst, AdminMenuObject parentItem)
        {
            return lst.Where(m => m.ParentId == parentItem.AdminMenuId).ToList();
        }

        private static string RecursionItems(IEnumerable<AdminMenuObject> lst, IEnumerable<AdminMenuObject> childLst)
        {            
            var ret = new StringBuilder();
            foreach (var m in childLst)
            {
                var childLst2 = GetChildItems(lst, m);
                if (childLst2.Count > 0)
                {
                    ret.AppendFormat("<li><a href=\"/Default.aspx?cId={0}\"><span></span>{1}</a></li><ul>", m.AdminMenuId, m.Name);
                    ret.Append(RecursionItems(lst, childLst2));
                    ret.Append("</ul></li>");
                }
                else
                {
                    if (m.Status != 2)
                        ret.AppendFormat("<li><a href=\"/Default.aspx?cId={0}\"><span></span>{1}</a></li>", m.AdminMenuId, m.Name);
                }
            }
            return ret.ToString();
        } 
        #endregion
    }
}